# pylint: option block-disable-msg
# graphgui - Testing of various geometric primitives

import pygame, time, math
from pygame.locals import *

import guimain
from pgEngine.point import Point, AxisAlignedBoundingBox, OrientedBoundingBox
from gui.resources import Resources

class GraphPoint(Point): # pylint: option block-disable-msg
    # For display purposes.
    def vertex(self,center,scale):
        return self * scale + center

class GraphAABB(AxisAlignedBoundingBox): # pylint: option block-disable-msg
    def vertexList(self,center,scale): # pylint: option block-disable-msg
        translation = self.radii
        # Rotate the radii by our angle
        ptY = Point.fromVector(self.angle + math.pi / 2, translation.y)
        ptX = Point.fromVector(self.angle, translation.x)
        return [ ((self.center + ptX + ptY) * scale + center).pos ,
                 ((self.center + ptX - ptY) * scale + center).pos,
                 ((self.center - ptX - ptY) * scale + center).pos,
                 ((self.center - ptX + ptY) * scale + center).pos ]
    
class GraphOBB(OrientedBoundingBox):# pylint: option block-disable-msg
    def vertexList(self,center,scale): # pylint: option block-disable-msg
        translation = self.radii
        # Rotate the radii by our angle
        ptY = Point.fromVector(self._angle + math.pi / 2, translation.y)
        ptX = Point.fromVector(self._angle, translation.x)
        return [ ((self.center + ptX + ptY) * scale + center).pos ,
                 ((self.center + ptX - ptY) * scale + center).pos,
                 ((self.center - ptX - ptY) * scale + center).pos,
                 ((self.center - ptX + ptY) * scale + center).pos ]

def drawGrid(gridSize,screenSize): # pylint: option block-disable-msg
    center = screenSize/2
    grid = pygame.Surface(screenSize.pos)
    lines = screenSize / gridSize
    maxLines = max(lines.x, lines.y)
    for n in range(-maxLines, maxLines, 1):
        if n != 0: color=(30,30,30)
        else: color=(80,80,80)
        pygame.draw.aalines(grid, color, True, [(n*gridSize.x+ center.x,0),
                                                (n*gridSize.x+ center.x,screenSize.y)], 1)
        pygame.draw.aalines(grid, color, True, [(0,n*gridSize.y + center.y),
                                                (screenSize.x,n*gridSize.y + center.y)], 1)
    return grid

def runGraph(): # pylint: option block-disable-msg   
    size = Point(800,600)
    unit = Point(30,30)
    scale = unit * Point(1.0, -1.0)
    grid = drawGrid(unit, size)
    center = (size/2)
    
    res = Resources()
    window = pygame.display.set_mode(size.pos,HWSURFACE|DOUBLEBUF)    
    offScreen = pygame.Surface(size.pos)
    
    pygame.font.init()
    font = res.loadFont("VeraMono.ttf")
    
    #abBox3 = GraphAABB(0.0, 0.0, 1.5, 1.5)
    #some3 = abBox3.vertexList(center,scale)
    
    a = 0
    while not pygame.event.peek(QUIT):
        pygame.event.clear()
        #TODO: Adapt this section to use Angle()?
        obBox2 = GraphOBB(Point(4.0,4.0), Point(1.0, 2.0), (240.0-a) * math.pi / 180.0)
        #pt2 = Point.fromVector((217-a) * math.pi / 360, 1.2)
        #pt2 += Point(3.0,2.0)
        obBox1 = GraphOBB(Point(3.0,2.0), Point(3.5,1.0), (125+a) * math.pi / 360.0)
        #distance1 = obBox1.dist(pt2)
        #result = obBox2.collision(obBox1)
        distance1 = obBox1.dist(obBox2)
        distance2 = distance3 = 0.0
        #distance3 = obBox1.dist(abBox3)
        if distance1 < 0.0: 
            color = (255,255,128)
        elif distance2 < 0.0:
            color = (128,255,255)
        elif distance3 < 0.0:
            color = (255,128,255)
        else:
            color = (160,160,160)
            
        srfDist1 = font.render("Distance: %-0.3f" % distance1, True, (200,200,200))
        srfDist2 = font.render("Distance: %-0.3f" % distance2, True, (200,200,200))
        srfDist3 = font.render("Distance: %-0.3f" % distance3, True, (200,200,200))
     
        offScreen.blit(grid,(0,0))
        offScreen.blit(srfDist1, (center.x - srfDist1.get_width() / 2,
                                  size.y - srfDist1.get_height() * 2))
        offScreen.blit(srfDist2, (center.x - srfDist2.get_width() / 2,
                                  size.y - srfDist2.get_height() * 3))
        offScreen.blit(srfDist3, (center.x - srfDist3.get_width() / 2,
                                  size.y - srfDist3.get_height() * 4))
        
        some = obBox1.vertexList(center,scale)
        pygame.draw.aalines(offScreen, color, True, some, 1)
        some2 = obBox2.vertexList(center,scale)
        pygame.draw.aalines(offScreen, color, True, some2, 1)
        #pygame.draw.circle(offScreen, color, (pt2 * scale + center).pos, 3)
        #pygame.draw.aalines(offScreen, color, True, some3, 1)
        #offScreen.set_at((pt2 * scale + center).pos, color)
        window.blit(offScreen,(0,0))
        pygame.display.flip()
        #time.sleep(0.01)
        a = (a+1) % 720

if __name__ == "__main__": # pylint: option block-disable-msg
    runGraph()
